package com.lemon.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lemon.domain.Category;
import com.lemon.domain.PageResult;
import com.lemon.domain.Sku;
import com.lemon.domain.Spu;
import com.lemon.domain.dto.SpuDto;
import com.lemon.domain.pageParam.SpuPageParam;
import com.lemon.mapper.CategoryMapper;
import com.lemon.mapper.SkuMapper;
import com.lemon.mapper.SpuMapper;
import com.lemon.mapper.SupplierMapper;
import com.lemon.service.SpuService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class SpuServiceImpl extends ServiceImpl<SpuMapper, Spu> implements SpuService {
    private final SpuMapper spuMapper;

    private final SkuMapper skuMapper;

    private final CategoryMapper categoryMapper;

    private final SupplierMapper supplierMapper;
    @Override
    @Transactional
    public void add(SpuDto spuDto) {
        Spu spu = new Spu();
        BeanUtils.copyProperties(spuDto,spu);
        spu.setCreateTime(LocalDateTime.now());
        Category category = categoryMapper.selectById(spuDto.getCategoryId3());
        spu.setCategoryId2(category.getParentId());
        Category category1 = categoryMapper.selectOne(Wrappers.<Category>lambdaQuery().eq(Category::getId, category.getParentId()));
        spu.setCategoryId1(category1.getParentId());
        spuMapper.insert(spu);
        List<Sku> children = spuDto.getChildren();
        children.forEach(sku -> {
            sku.setSpuId(spu.getId());
            sku.setCreateTime(LocalDateTime.now());
            sku.setProductVolume(sku.getProductLength()*sku.getProductWidth()*sku.getProductHeight());
            skuMapper.insert(sku);
        });
    }
    @Override
    @Transactional
    public void updateSpu(SpuDto spuDto) {
        Spu spu = new Spu();
        BeanUtils.copyProperties(spuDto,spu);
        spu.setUpdateTime(LocalDateTime.now());
        spuMapper.updateById(spu);
        skuMapper.delete(Wrappers.<Sku>lambdaQuery().eq(Sku::getSpuId,spu.getId()));
        spuDto.getChildren().forEach(sku -> {
            sku.setSpuId(spu.getId());
            sku.setCreateTime(LocalDateTime.now());
            sku.setUpdateTime(LocalDateTime.now());
            sku.setProductVolume(sku.getProductLength()*sku.getProductWidth()*sku.getProductHeight());
            skuMapper.insert(sku);
        });
    }

    @Override
    @Transactional
    public void deleteSpu(Integer id) {
        spuMapper.deleteById( id);
        skuMapper.delete(Wrappers.<Sku>lambdaQuery().eq(Sku::getSpuId,id));
    }

    @Override
    public List<Sku> getSkuList() {
        List<Sku> list = skuMapper.selectList(null);
        return list;
    }

    @Override
    public PageResult getAll(SpuPageParam spuPageParam) {
        PageHelper.startPage(spuPageParam.getPage(),spuPageParam.getPageSize());
        List<SpuDto> spuList =  spuMapper.getAll(spuPageParam);
        spuList.forEach(spuDto -> {
            spuDto.setChildren(skuMapper.selectList(Wrappers.<Sku>lambdaQuery().eq(Sku::getSpuId,spuDto.getId())));
            Category category = categoryMapper.selectById(spuDto.getCategoryId3());
            spuDto.setCategoryName(category.getName());
            spuDto.setSupplierName(supplierMapper.selectById(spuDto.getSupplierId()).getSupplierName());
        });
        Page page = (Page) spuList;
        return new PageResult(page.getTotal(),page.getResult());
    }
}
